package com.zr.service.order.impl;

import com.zr.dao.order.OrderGoodsDao;
import com.zr.service.order.OrderGoodsService;
import com.zr.vo.order.OrderGoods;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单货物处理
 */
@Service
public class OrderGoodsServiceImpl implements OrderGoodsService {

    @Resource
    private OrderGoodsDao orderGoodsDao;

    // 注入默认订单编号
    @Value("${orderId.default}")
    private  String orderIdDefault;

    /**
     * 查询货物列表
     * @param goodsOrderId
     * @return Map<String, Object>
     * @author huangguilong
     */
    @Override
    public Map<String, Object> getGoodsByOrderId(String goodsOrderId) {
        List<OrderGoods> orderGoodsList= orderGoodsDao.selectGoodsListByOrderId(goodsOrderId);
        Float weightTotal = 0.0F;
        Float volumeTotal = 0.0F;
        // 计算订单货物总重量和总体积
        for(OrderGoods orderGoods : orderGoodsList){
            weightTotal += orderGoods.getGoodsWeight();
            volumeTotal += orderGoods.getGoodsVolume();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("weightTotal",weightTotal);
        map.put("volumeTotal",volumeTotal);
        map.put("orderGoodsList",orderGoodsList);
        return map;
    }

    /**
     * 新增订单货物
     * @param orderGoods
     * @return int
     * @author huangguilong
     */
    @Override
    public int add(OrderGoods orderGoods) {
        return orderGoodsDao.insertSelective(orderGoods);
    }

    /**
     * 编辑订单货物
     * @param orderGoods
     * @return int
     * @author huangguilong
     */
    @Override
    public int edit(OrderGoods orderGoods) {
        return orderGoodsDao.updateByPrimaryKeySelective(orderGoods);
    }

    /**
     * 删除订单货物
     * @param orderGoods
     * @return int
     * @author huangguilong
     */
    @Override
    public int remove(OrderGoods orderGoods) {
        return orderGoodsDao.deleteByPrimaryKey(orderGoods.getId());
    }

    /**
     * 删除所有未生成委托订单的货物信息
     * @return int
     * @author huangguilong
     */
    @Override
    public int deleteAllByOrderId() {
        // orderIdDefault 注入为 WT************
        return orderGoodsDao.deleteAllByOrderId(orderIdDefault);
    }

}
